import { Injectable } from "@nestjs/common";
import { CharacterTrainingService } from "../services/character-training.service";

export type CharacterTrainingWsHandler = (
  payload: any,
  helpers: any
) => Promise<any>;

@Injectable()
export class CharacterTrainingWsHandlers {
  constructor(
    private readonly characterTrainingService: CharacterTrainingService
  ) {}

  /**
   * 处理主将训练指令
   */
  async handleCharacterTrainingCommand(
    command: number,
    payload: any,
    helpers: any
  ): Promise<any> {
    const { selectedCharacterId } = helpers;

    if (!selectedCharacterId) {
      return {
        act_id: command,
        code: -1,
        msg: "请先选择角色",
      };
    }

    switch (command) {
      case 1204:
        return await this.handleGetTrainingInfo(selectedCharacterId, payload);

      case 1205:
        return await this.handleGetTrainingDetails(selectedCharacterId);

      case 1207:
        return await this.handleStartTraining(selectedCharacterId, payload);

      case 1208:
        return await this.handleClaimTrainingReward(selectedCharacterId);

      default:
        return {
          act_id: command,
          code: -1,
          msg: "未知的训练指令",
        };
    }
  }

  /**
   * 处理获取训练信息 (1204)
   */
  private async handleGetTrainingInfo(
    characterId: number,
    payload: any
  ): Promise<any> {
    const typeParam = payload && typeof payload.t === "number" ? payload.t : 1;
    return await this.characterTrainingService.getTrainingInfo(
      characterId,
      typeParam
    );
  }

  /**
   * 处理获取训练详情 (1205)
   */
  private async handleGetTrainingDetails(characterId: number): Promise<any> {
    return await this.characterTrainingService.getTrainingDetails(characterId);
  }

  /**
   * 处理开始训练 (1207)
   */
  private async handleStartTraining(
    characterId: number,
    payload: any
  ): Promise<any> {
    const { h: hours } = payload;

    if (!hours || typeof hours !== "number") {
      return {
        act_id: 1207,
        code: -1,
        msg: "请指定训练小时数",
      };
    }

    return await this.characterTrainingService.startTraining(
      characterId,
      hours
    );
  }

  /**
   * 处理领取训练奖励 (1208)
   */
  private async handleClaimTrainingReward(characterId: number): Promise<any> {
    return await this.characterTrainingService.claimTrainingReward(characterId);
  }
}

// 导出处理器映射
export const characterTrainingWsHandlers: Record<
  number,
  CharacterTrainingWsHandler
> = {
  // 1204: 获取训练信息
  1204: async (payload, helpers) => {
    const trainingHandlers = new CharacterTrainingWsHandlers(
      helpers.characterTrainingService
    );
    return await trainingHandlers.handleCharacterTrainingCommand(
      1204,
      payload,
      helpers
    );
  },

  // 1205: 获取训练详情
  1205: async (payload, helpers) => {
    const trainingHandlers = new CharacterTrainingWsHandlers(
      helpers.characterTrainingService
    );
    return await trainingHandlers.handleCharacterTrainingCommand(
      1205,
      payload,
      helpers
    );
  },

  // 1207: 开始训练
  1207: async (payload, helpers) => {
    const trainingHandlers = new CharacterTrainingWsHandlers(
      helpers.characterTrainingService
    );
    return await trainingHandlers.handleCharacterTrainingCommand(
      1207,
      payload,
      helpers
    );
  },

  // 1208: 领取训练奖励
  1208: async (payload, helpers) => {
    const trainingHandlers = new CharacterTrainingWsHandlers(
      helpers.characterTrainingService
    );
    return await trainingHandlers.handleCharacterTrainingCommand(
      1208,
      payload,
      helpers
    );
  },
};
